import exception.NoEnoughPapersException;
import strategy.OrderSelectionStrategy;
import visitor.StudentCountVisitor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Client {
    public static void main(String[] args) {
        // 初始化论文清单
        List<Paper> paperList = new ArrayList<>();
        paperList.add(new Paper.Thesis("Thesis Paper 1"));
        paperList.add(new Paper.ConferencePaper("Conference Paper 1"));
        paperList.add(new Paper.JournalPaper("Journal Paper 1"));
        paperList.add(new Paper.Thesis("Thesis Paper 2"));
        final PaperList listOfRainy = new PaperList("Rainy", paperList);

        // 初始化学生列表
        final List<Student> students = Arrays.asList(
                new Student("Cloudy", 1, PaperType.Thesis),
                new Student("Sunny", 1, PaperType.JournalPaper),
                new Student("Windy", 2, PaperType.Any)
        );

        // 测试不同选择策略，这里设置为按顺序选择策略示例
        listOfRainy.setStrategy(new OrderSelectionStrategy());

        // 多线程模拟学生选择论文
        for (Student student : students) {
            new Thread(() -> {
                try {
                    listOfRainy.selectPaperByRandom(student);
                } catch (NoEnoughPapersException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }

        // 等待线程执行完毕（简单方式，实际可优化用 CountDownLatch 等）
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 输出选择结果
        PaperSelection selectionResult = listOfRainy.getSelectionResult();
        System.out.println(selectionResult.toString());

        // 测试 Visitor 模式，统计选至少1篇的学生数
        StudentCountVisitor visitor = new StudentCountVisitor();
        selectionResult.accept(visitor);
        int studentCount = (int) visitor.getResult();
        System.out.println("选至少1篇论文的学生数量：" + studentCount);
    }
}